﻿using DropBoxClient.Entities;
using DropBoxClient.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace DropBoxClient.Operations
{
    internal class PutFilesOperation
    {
        private static readonly string Url = "https://api-content.dropbox.com/1/files_put/{0}/{1}";
        private static readonly string MethodType = StringConstants.PutMethodType;

        private string _accessToken;
        private WebOperation<Meta> _operation;

        internal string Root { get; set; }
        internal string Path { get; set; }

        private string _inputPath;
        internal string InputPath
        {
            get { return _inputPath; }
            set
            {
                _inputPath = value;
                InputStream = File.Open(_inputPath, FileMode.Open, FileAccess.Read);
            }
        }

        internal Stream InputStream { get; set; } 

        internal PutFilesOperation(string accessToken)
        {
            _accessToken = accessToken;
        }

        internal Meta Execute()
        {
            string url = string.Format(Url, Root, Path);
            _operation = new WebOperation<Meta>(url, _accessToken, MethodType, null, InputStream);
            return _operation.Execute();
        }
    }
}
